package water.com.DaoSupport.DbBase;

import water.com.DaoSupport.entity.User;
import water.com.Ioc.WaterBeanFactory;
import water.com.Ioc.WaterBeanRegistryInfo;
import org.junit.Test;
import water.com.Log.Log;

import java.util.*;

/**
 * 此类作用升级为DB自检
 */
public class DbTest {

    /**
     * DB启动自检方法
     */
    public static void DBStartTest(){
        Log.getLog().info("DB自检开始");
       Map<String,String> DbMap =  WaterBeanRegistryInfo.registryMap;
       for(Map.Entry<String,String> entry:DbMap.entrySet()){
           String value = entry.getValue();
           try {
               if(value.contains("Dao.entity")){
                   Object obj = WaterDaoSupportImp.getInstance().selectOne(WaterBeanFactory.singleBeanFactory.get(entry.getKey()));
               }
           }catch (Exception e){
               e.printStackTrace();
           }
       }
        Log.getLog().info("DB自检结束");
    }

    @Test
    public void TestInsertOne(){
        try {
            WaterDaoSupportImp.getInstance().insertOne(makeOneUsers());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Test
    public void TestSelectOne(){
        User user = new User();
        user.setId("89898");
        try {
            user = (User) WaterDaoSupportImp.getInstance().selectOne(user);
        }catch (Exception e){
            e.printStackTrace();
        }

        System.out.println(user.toString());
    }

    @Test
    public void TestUpdateOne(){
        User user = new User();
        user.setId("89898");
        try {
            user = (User) WaterDaoSupportImp.getInstance().selectOne(user);
        }catch (Exception e){
            e.printStackTrace();
        }
        user.setUname("四郎");
        Map<String,String> condition = new HashMap<>();
        condition.put("Id","89898");
        try {
            WaterDaoSupportImp.getInstance().updateOne(user,condition);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Test
    public void TestDeleteOne(){
        User user = new User();
        user.setId("89898");
        try {
            WaterDaoSupportImp.getInstance().deleteOne(user);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public User makeOneUsers(){
        User user = new User();
        user.setCode("1204555");
        user.setUname("严牧之");
        user.setPassword("888888");
        user.setId("89898");
      return user;
    }

    @Test
    public void TestInsertList(){
        try {
            WaterDaoSupportImp.getInstance().insertList(makeListUsers(50));
        }catch (Exception e){

        }

    }

    @Test
    public void TestSelectList(){
        User user= new User();
        user.setCode("测试数据");
        try {
            List<User> userList = WaterDaoSupportImp.getInstance().selectList(user);
            for(User users: userList){
                System.out.println(users.toString());
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Test
    public void TestUpdateList(){
        User user= new User();
        user.setCode("测试数据");
        try {
            List<User> userList = WaterDaoSupportImp.getInstance().selectList(user);
            List<Map> conditions = new ArrayList<>();
            for(User users: userList){
                String uname = user.getUname();
                users.setUname(user.getPassword());
                users.setPassword(uname);
                Map<String,String> condition = new HashMap<>();
                condition.put("id",users.getId());
                conditions.add(condition);
            }
            Integer num = (Integer) WaterDaoSupportImp.getInstance().updateList(userList,conditions);
            System.out.println(num);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Test
    public void TestDeleteList(){
        List<User> userList = new ArrayList<>();
        for(int i=0;i<50;i++){
            User user= new User();
            user.setId(String.valueOf(i));
            userList.add(user);
        }
        try {
            WaterDaoSupportImp.getInstance().deleteList(userList);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public List<User> makeListUsers(int n){
        List<User> userList = new ArrayList<>();
        for(int i=0;i<n;i++){
            User user = new User();
            user.setId(String.valueOf(i));
            user.setCode("测试数据");
            user.setPassword("四郎"+i);
            user.setUname("牧之"+i);
            userList.add(user);
        }
        return userList;
    }


}
